package com.zhipu.balance.ui.vm.home.calculate.rapeseed

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRapeseed
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 油菜籽生霉粒
 */
class RapeseedMildewViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 油菜籽实体
     */
    var mRecordRapeseed : RecordRapeseed? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 生霉粒重量
     */
    var moldWeight = ObservableField("")

    /**
     * 大样杂质率
     */
    var detailResults = ObservableField("")
    /**
     * 小样试验-试样质量
     */
    var smallMiscellaneousSampleQuality = ObservableField("")

    /**
     * 生霉粒率-结果
     */
    var moldResult = ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRapeseed?.moldRecorder)) {
            //生霉粒重量
            moldWeight.set(mRecordRapeseed?.moldWeight.toString())
            //大样杂质率
            detailResults.set(mRecordRapeseed?.detailResults.toString())
            //小样试验-试样质量
            smallMiscellaneousSampleQuality.set(mRecordRapeseed?.smallMiscellaneousSampleQuality.toString())
            //生霉粒率-结果
            moldResult.set(mRecordRapeseed?.moldResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //生霉粒重量
        moldWeight.set("")
        //大样杂质率
        detailResults.set("")
        //小样试验-试样质量
        smallMiscellaneousSampleQuality.set("")
        //生霉粒率-结果
        moldResult.set("")
    }


    /**
     * 大样质量
     */
    fun BulkQuality(){
        moldWeight.set(weighing)
        mRecordRapeseed?.moldWeight = moldWeight.get()?.toDouble()
    }


    /**
     * 刷新数据
     */
    fun refreshData(){
        //小样重量
        smallMiscellaneousSampleQuality.set(mRecordRapeseed!!.smallMiscellaneousSampleQuality.toString())
        //大样杂质率
        detailResults.set(mRecordRapeseed!!.detailResults.toString())
    }




    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算大样杂质率
         */
        var details = 100 - mRecordRapeseed!!.detailResults
        /**
         * 计算小样
         */
        var smallKind =  mRecordRapeseed!!.moldWeight / mRecordRapeseed!!.smallMiscellaneousSampleQuality
        /**
         * 生芽粒率-结果
         */
        var totalImpurity =CommonUtils.formatDouble(details * smallKind)
        mRecordRapeseed?.moldResult = totalImpurity
        moldResult.set(totalImpurity.toString())

        /**
         * 生霉粒率记录员
         */
        mRecordRapeseed!!.moldRecorder = mPersonnel!!.name


        //生霉粒重量
        if(mRecordRapeseed?.moldWeight == 0.00){
            moldWeight.set("0.00")
        }

    }


}